<template>
  <div class="team-list-container">
    <!-- 搜索框 -->
    <div class="search-box">
      <h2 class="search-title">专业搬家团队，服务效率到位。</h2>
      <div class="search-input">
        <el-input v-model="searchText" placeholder="请输入关键字开始寻找服务项目">
          <template #append>
            <el-button type="primary" @click="loadTeamList">开始搜索</el-button>
          </template>
        </el-input>
      </div>
    </div>

    <!-- 搜索结果统计 -->
    <div class="search-result">
      已为您搜到 <span class="highlight">{{ total }}</span> 个优质团队
    </div>

    <!-- 团队列表 -->
    <div class="team-list">
      <div v-for="team in teamList" :key="team.id" class="team-item" @click="goToDetail(team.id)">
        <div class="team-image">
          <el-image :src="team.team_img" fit="cover"/>
        </div>
        <div class="team-info">
          <h3 class="team-name">{{ team.team_name }}</h3>
          <p class="team-desc">{{ team.content }}</p>
          <p class="team-time">发布时间: {{ formatTime(team.create_time) }}</p>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          layout="prev, pager, next"
          @current-change="handlePageChange"
      />
      <span class="page-info">共 {{ total }} 条 {{ pageSize }}条/页</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { get } from '@/net'
import dayjs from 'dayjs'

const router = useRouter()
const searchText = ref('')
const teamList = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(3)

// 加载团队列表
const loadTeamList = () => {
  get(`/api/team/getTeamList/${currentPage.value}?keywords=${searchText.value}`, (res) => {
    teamList.value = res.list
    total.value = res.total
  })
}

// 格式化时间
const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

// 跳转到详情页
const goToDetail = (teamId) => {
  router.push(`/user/user_team_detail/${teamId}`)
}

// 处理分页变化
const handlePageChange = (page) => {
  currentPage.value = page
  loadTeamList()
}

onMounted(() => {
  loadTeamList()
})
</script>

<style scoped>
.team-list-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.search-box {
  text-align: center;
  margin-bottom: 30px;
}

.search-title {
  margin-bottom: 20px;
}

.search-input {
  max-width: 600px;
  margin: 0 auto;
}

.search-result {
  margin: 20px 0;
  font-size: 16px;
}

.highlight {
  color: #409EFF;
  font-weight: bold;
}

.team-list {
  margin-bottom: 30px;
}

.team-item {
  display: flex;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid #eee;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.team-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.team-image {
  width: 200px;
  height: 150px;
  margin-right: 20px;
}

.team-info {
  flex: 1;
}

.team-name {
  font-size: 18px;
  margin-bottom: 10px;
}

.team-desc {
  color: #666;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.team-time {
  color: #999;
  font-size: 14px;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
}

.page-info {
  color: #666;
  font-size: 14px;
}
</style>